package com.cqc.jdk8.Jtime;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @author cui
 * @date 2024/6/29 19:37
 * @description
 **/
public class LocalDateTest {

    @Test
    public void getField() {
        //今天是 2022-5-24 周二
        LocalDate date = LocalDate.now();
        int year = date.getYear();
        int monthValue = date.getMonthValue();
        int dayOfMonth = date.getDayOfMonth();
        int dayOfYear = date.getDayOfYear();
        int dayOfWeek = date.getDayOfWeek().getValue();
        //2022-5-24-144-2
        System.out.println(year + "-" + monthValue + "-" + dayOfMonth + "-" + dayOfYear + "-" + dayOfWeek);
    }


    //计算加减
    @Test
    public void compute() {
        //今天是2022-05-24
        LocalDate date = LocalDate.now();
        //减
        System.out.println(date.minusYears(1).toString());//2021-05-24
        System.out.println(date.minusMonths(1).toString());//2022-04-24
        System.out.println(date.minusWeeks(1).toString());//2022-05-17
        System.out.println(date.minusDays(1).toString());//2022-05-23
        System.out.println("-----------------------------------------------");
        //加
        System.out.println(date.plusYears(1).toString());//2023-05-24
        System.out.println(date.plusMonths(1).toString());//2022-06-24
        System.out.println(date.plusWeeks(1).toString());//2022-05-31
        System.out.println(date.plusDays(1).toString());//2022-05-25
        System.out.println(date.plus(1, ChronoUnit.WEEKS).toString());//2022-05-31
    }

    //格式化
    @Test
    public void format() {
        LocalDate date = LocalDate.now();
        System.out.println("toString=" + date.toString());//2022-05-24
        System.out.println("BASIC_ISO_DATE=" + date.format(DateTimeFormatter.BASIC_ISO_DATE));//20220524
        System.out.println("ISO_DATE=" + date.format(DateTimeFormatter.ISO_DATE));//2022-05-24
        System.out.println("ISO_LOCAL_DATE=" + date.format(DateTimeFormatter.ISO_LOCAL_DATE));//2022-05-24
        System.out.println("自定义格式=" + date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));//2022-05-24

    }

    //解析
    @Test
    public void parse() {
        LocalDate date = LocalDate.parse("2022-01-02", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(date.toString());//2022-01-02
        //注意：末尾会省略00，如果yMHms改成中文就不会省略
        LocalTime time = LocalTime.parse("10:30:00", DateTimeFormatter.ofPattern("HH:mm:ss"));
        System.out.println(time.toString());//10:30

    }



    /**
     * 判断2个日期的大小
     */
    @Test
    public void isAfterOrIsBefore() {
        LocalDate date1 = LocalDate.of(2022, 9, 2);
        LocalDate date2 = LocalDate.of(2023, 9, 2);
        boolean after = date1.isAfter(date2);//false
        boolean before = date1.isBefore(date2);//true
        System.out.println(after + ", " + before);
    }

    /**
     * 是不是闰年
     */
    @Test
    public void isLeapYear() {
        LocalDate date1 = LocalDate.of(2022, 9, 2);
        boolean leapYear = date1.isLeapYear();
        System.out.println("闰年：" + leapYear);
    }


    /**
     * 给LocalDate赋值time,转成LocalDateTime
     */
    @Test
    public void atTime() {
        LocalDate now = LocalDate.now();
        LocalDateTime min = now.atTime(LocalTime.MIN);
        LocalDateTime noon = now.atTime(LocalTime.NOON);
        LocalDateTime midNight = now.atTime(LocalTime.MIDNIGHT);
        LocalDateTime max = now.atTime(LocalTime.MAX);
        LocalDateTime dateTime1 = now.atTime(23, 59, 59);

        System.out.println("min=" + min.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));//2024-06-29 00:00:00.000
        System.out.println("noon=" + noon.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));//2024-06-29 12:00:00.000
        System.out.println("midNight=" + midNight.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));//2024-06-29 00:00:00.000
        System.out.println("max=" + max.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));//2024-06-29 23:59:59.999
        System.out.println("dateTime1=" + dateTime1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));//2024-06-29 23:59:59.000

        //没有  .atEndOfDay();
        LocalDateTime dateTime3 = now.atStartOfDay();
        //2024-04-21 00:00:00
        System.out.println(dateTime3.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }


    /**
     * 判断是否相等
     */
    @Test
    public void equals() {
        LocalDate date1 = LocalDate.of(2024, 6, 30);
        LocalDate date2 = LocalDate.now();
        System.out.println("isEqual：" + date1.isEqual(date2));
        System.out.println("equals：" + date1.equals(date2));
    }
}
